import pymysql
import configparser


class UserDao:
    def __init__(self):
        config = configparser.ConfigParser()
        config.read('setting.ini')
        if config.get('mode', 'mode') == 'debug':
            host = config.get('debug', 'host')
            user = config.get('debug', 'user')
            password = config.get('debug', 'password')
            db = config.get('debug', 'database')
        else:
            host = config.get('release', 'host')
            user = config.get('release', 'user')
            password = config.get('release', 'password')
            db = config.get('release', 'database')

        self.conn = pymysql.connect(host=host, user=user, password=password, db=db, charset='utf8')
        self.cursor = self.conn.cursor()

    def select_user_by_username(self, username):
        sql = "select * from user where user_name = '%s'" % username
        self.cursor.execute(sql)
        result = self.cursor.fetchone()
        return result

    def select_user_by_id(self, user_id):
        sql = "select * from user where user_id = '%s'" % user_id
        self.cursor.execute(sql)
        result = self.cursor.fetchone()
        return result

    def insert_user(self, username, password):
        sql = "insert into user(user_name, password) values('%s', '%s')" % (username, password)
        self.cursor.execute(sql)
        self.conn.commit()

    def update_user(self, user_id, username, password):
        sql = "update user set user_name = '%s', password = '%s' where user_id = '%s'" % (username, password, user_id)
        self.cursor.execute(sql)
        self.conn.commit()

    def delete_user(self, user_id):
        sql = "delete from user where user_id = '%s'" % user_id
        self.cursor.execute(sql)
        self.conn.commit()

        